﻿package com.find.memories.util;

import java.util.Iterator;
import java.util.List;

import android.app.ActivityManager;
import android.content.ContentResolver;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.os.Build;
import android.os.Build.VERSION_CODES;
import android.provider.MediaStore;
import android.view.Display;
import android.view.WindowManager;

import com.find.memories.gallery.PhotoItem;

public class Utils {

    public static boolean hasFroyo() {
        // Can use static final constants like FROYO, declared in later versions
        // of the OS since they are inlined at compile time. This is guaranteed
        // behavior.
        return Build.VERSION.SDK_INT >= VERSION_CODES.FROYO;
    }

    public static boolean hasGingerbread() {
        return Build.VERSION.SDK_INT >= VERSION_CODES.GINGERBREAD;
    }

    public static boolean hasHoneycomb() {
        return Build.VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB;
    }

    public static boolean hasHoneycombMR1() {
        return Build.VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB_MR1;
    }

    public static boolean hasJellyBean() {
        return Build.VERSION.SDK_INT >= 16;
    }

    public static boolean hasKitKat() {
        return Build.VERSION.SDK_INT >= 19;
    }

    public static int getImageOrientation(String paramString) {
        int i = 0;
        if (paramString == null)
            return 0;

        try {
            int j = new ExifInterface(paramString).getAttributeInt("Orientation", 0);
            if (j == 6)
                i = 90;
            else if (j == 3)
                i = 180;
            else if (j == 8)
                i = 270;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return i;
    }

    public static Bitmap getRotatedBitmap(Bitmap paramBitmap, float paramFloat) {
        try {
            Matrix localMatrix = new Matrix();
            localMatrix.postRotate(paramFloat);
            Bitmap localBitmap = Bitmap.createBitmap(paramBitmap, 0, 0, paramBitmap.getWidth(), paramBitmap.getHeight(), localMatrix, true);
            return localBitmap;
        } catch (Exception localException) {
            return null;
        }
    }
    
    public static Bitmap getThumnailImg(ContentResolver cr, PhotoItem item) {
        BitmapFactory.Options options       =   new BitmapFactory.Options();
        options.inSampleSize                        = 1;
        
        Bitmap thumbnail = MediaStore.Images.Thumbnails.getThumbnail(cr, Long.valueOf(item.id), MediaStore.Images.Thumbnails.MINI_KIND, options);
        
        int ret = Utils.getImageOrientation(item.data);
        
        if (ret != 0)
            thumbnail = Utils.getRotatedBitmap(thumbnail, ret);
        
        return thumbnail;
    }
    
    public static int getIndex(String[] strArray, String value) {
        return getIndex(strArray, value, 0);
    }
    
    public static int getIndex(String[] strArray, String value, int defaultValue) {
        if (value == null || "".equals(value))
            return 0;
        
        for (int idx=0; idx<strArray.length; idx++) {
            if (value.equals(strArray[idx]))
                return idx;
        }
        
        return 0;
    }
    
    public static int getDeviceWidth(Context context) {
        Display disp = ((WindowManager)context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        return disp.getWidth();
    }
    
    public static boolean checkRunningService(Context paramContext,
            String paramString) {
        List info = ((ActivityManager) paramContext
                .getSystemService("activity")).getRunningServices(200);
        for (Iterator localIterator = info.iterator(); localIterator.hasNext();) {
            if (((ActivityManager.RunningServiceInfo) localIterator.next()).service
                    .getClassName().equalsIgnoreCase(paramString))
                return true;
        }
        return false;
    }
}
